#!/usr/bin/python

import pygst
pygst.require("0.10")
import gst
import gobject 

class RecorderBin(gst.Bin):
    '''record stream while playing....
    
			    -->vorbisenc-->oggmux-->filesink            #save stream to file.
    audioconvert-->mymux-->
			    -->queue-->audioconvert-->alsasink	#push stream to audio card to play it.
    '''
    def __init__(self,output):
	self.__gobject_init__()
	conv=gst.element_factory_make("audioconvert", "converter")
	mymux=gst.element_factory_make("tee")
	self.add(conv,mymux)
	conv.link(mymux)
	
	enc = gst.element_factory_make("vorbisenc", "vorbis-encoder")
	create = gst.element_factory_make("oggmux", "ogg-create")
	fileout = gst.element_factory_make("filesink", "sink")
	fileout.set_property("location", "/home/shrek/radio.ogg")
	self.add(enc,create,fileout)		
	gst.element_link_many(enc,create,fileout)	
	
	alsaconv=gst.element_factory_make('audioconvert')
	alsaqueue=gst.element_factory_make('queue')
	alsasink=gst.element_factory_make('alsasink')
	self.add(alsaconv,alsaqueue,alsasink)
	
	mymux.link(enc)
	gst.element_link_many(mymux,alsaqueue,alsaconv,alsasink)
	
	self.add_pad(gst.GhostPad('sink',conv.get_pad('sink')))
class MyMux(gst.Element):
    __gstdetails__ = ('My Plugin',
                    "tee",
                    "Simple Tee Plugin",
                    "Corey O.")
    _sinkpadtemplate = gst.PadTemplate ("sink",
	gst.PAD_SINK,
	gst.PAD_ALWAYS,
	gst.caps_new_any())
    _srcpadtemplate = gst.PadTemplate ("src%d",
	gst.PAD_SRC,
	gst.PAD_REQUEST,
	gst.caps_new_any())
    __gsttemplates__ = (_srcpadtemplate, _sinkpadtemplate)
    def __init__(self):
	gst.Element.__init__(self)
	self.srcpadcount=0
	self.srcpads=[]
	
	gst.info('creating sinkpad')
        self.sinkpad = gst.Pad(self._sinkpadtemplate)	
        self.sinkpad.set_chain_function(self.chainfunc)
        self.sinkpad.set_event_function(self.eventfunc)
        self.sinkpad.set_getcaps_function(gst.Pad.proxy_getcaps)
        self.sinkpad.set_setcaps_function(gst.Pad.proxy_setcaps)
	self.add_pad(self.sinkpad)
	
    def do_request_new_pad(self,template,name=None):
	#gst.info(name)	
	name="src%d"%self.srcpadcount
	self.srcpadcount+=1
	pad=gst.Pad(template,name)
	pad.set_event_function(self.srceventfunc)
	pad.set_query_function(self.srcqueryfunc)
	pad.set_getcaps_function(gst.Pad.proxy_getcaps)
        pad.set_setcaps_function(gst.Pad.proxy_setcaps)
	
	self.srcpads.append(pad)
	self.add_pad(pad)	
	return pad
    def do_release_pad(self,pad):
	self.srcpads.pop(self.srcpads.index(pad))
	self.remove_pad(pad)	
	
    def chainfunc(self,pad,buffer):
	if len(self.srcpads)==0:
	    return gst.FLOW_NOT_LINKED
	else:	    
	    for srcpad in self.srcpads:
		outbuf = buffer.copy_on_write ()
		status=srcpad.push(outbuf)
		if status!=gst.FLOW_OK:
		    return status
	    return gst.FLOW_OK
    def eventfunc(self,pad,event):
	for srcpad in self.srcpads:
	    status=srcpad.push_event(event)
	return status
    def srcqueryfunc (self, pad, query):
        return self.sinkpad.query (query)
    def srceventfunc (self, pad, event):
        return self.sinkpad.push_event (event)
	
gobject.type_register(MyMux)
gst.element_register(MyMux,'mymux')